﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace RaSuperGrid.SuperGrid.Adorners
{
    public class TemplateAdorner : Adorner 
    { 
        private readonly ContentPresenter contentPresenter;
        public TemplateAdorner(FrameworkElement adornedElement, object content, DataTemplate template)
            : base(adornedElement) 
        { 
            IsHitTestVisible = false; 
            contentPresenter = new ContentPresenter(); 
            contentPresenter.Content = content; 
            contentPresenter.ContentTemplate = template;
            contentPresenter.DataContext = adornedElement.DataContext;
        } 

        protected override int VisualChildrenCount 
        { 
            get { return 1; } 
        } 
        
        protected override Visual GetVisualChild(int index) 
        { 
            return contentPresenter; 
        } 
        
        protected override Size MeasureOverride(Size constraint) 
        { 
            contentPresenter.Measure(AdornedElement.RenderSize); 
            return AdornedElement.RenderSize; 
        } 
        
        protected override Size ArrangeOverride(Size finalSize) 
        { 
            contentPresenter.Arrange(new Rect(finalSize)); 
            return finalSize; 
        } 
    }
}
